package com.cf.r1029_e;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

public class e {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        in.nextToken(); int T = (int) in.nval;
        while (T-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        in.nextToken(); int n = (int) in.nval;
        long[] a = new long[n];
        long[] b = new long[n];
        for (int i = 0; i < n; i++) {
            in.nextToken(); a[i] = (long) in.nval;
        }
        for (int i = 0; i < n; i++) {
            in.nextToken(); b[i] = (long) in.nval;
        }
        if (a[n - 1] == b[n - 1]) {
            out.println(n);
            return;
        }

        int ans = -1;
        Set<Long> set = new HashSet<>();
        for (int i = n - 2; i >= 0; i--) {
            if (a[i] == b[i] || a[i] == a[i + 1] || b[i] == b[i + 1] || set.contains(a[i]) || set.contains(b[i])) {
                ans = i;
                break;
            }
            set.add(a[i + 1]);
            set.add(b[i + 1]);
        }

        out.println(ans + 1);
    }
}
